<?php
	
	$_userid = $_userid == 1 && $userid  ? $userid : $_userid; // 管理员编辑其他会员信息权限
	
//require_once 'includes/common.php';
require_once 'includes/global.func.php';

cache_page_start();
require_once 'includes/company.class.php';
$template = 'company';
$readproid = get_cookie('readproid');
if(intval($readproid))$prowhere = $readproid;
$catid = intval($catid);
if($head['title'])$head['title'] .= '_';
if($catid)
{
	$CSEO = cache_read('category_'.$catid.'.php');
	extract($CSEO);
	$head['title'] .= $meta_title.'_';
	$head['keywords'] = $meta_keywords.'_';
	$head['description'] = $meta_description.'_';	
}



	switch($action){
	
		case 'elite':
			if(!$id) showmessage('请选择要操作的会员');
			$company->elite($id,$status);
			showmessage('操作成功！', $forward);
		break;
		
		case 'manage':
			if(!$_userid) showmessage('您还没有登陆，即将跳转到登陆页面',url("?module=member&action=login&forward=".urlencode(URL)));
			// common
			if(!isset($action)) $action = 'manage';
			require_once 'form.class.php';
			// common
	
			$modelid = get_modelid();
			require CACHE_MODEL_PATH.'member_output.class.php';
			require_once MOD_ROOT.'member/includes/member.class.php';
			$member = new member();
			$data = $member->get_model_info($userid, $modelid);
			$member_output = new member_output($modelid, $userid);
			$forminfos = $member_output->get($data);
			include template('company_manage.html','yellowpage');
			
		break;
	
		case 'edit':
			if(!$_userid) showmessage('您还没有登陆，即将跳转到登陆页面',url("?module=member&action=login&forward=".urlencode(URL)));
			// common
			if(!isset($action)) $action = 'manage';
			require_once 'form.class.php';
			// common
			
			$modelid = get_modelid();
			require CACHE_MODEL_PATH.'member_output.class.php';
			/*require MOD_ROOT.'member/includes/member.class.php';
			$member = new member();*/
			require CACHE_MODEL_PATH.'member_form.class.php';
			$member_form = new member_form($modelid);
			if($dosubmit)
			{
				require CACHE_MODEL_PATH.'member_input.class.php';
				$member_input = new member_input($modelid);
				require CACHE_MODEL_PATH.'member_update.class.php';
				$member_update = new member_update($modelid, $_userid);
				
				$inputinfo = $member_input->get($info);
				if(isset($info) && is_array($info))
				{
					if(!$member->edit($info)) showmessage($member->msg());
				}
				$modelinfo = $inputinfo['model'];
				
				if($modelinfo)
				{
					$modelinfo['userid'] = $_userid;
					$member_update->update($modelinfo);
					$member->edit_model($modelid, $modelinfo);
				}
				$byear = intval($byear);
				$bmonth = intval($bmonth);
				$employnum = htmlspecialchars($employnum);
				$turnover = htmlspecialchars($turnover);
				$telephone = htmlspecialchars($telephone);
				
				$byear = intval($byear);
				$byear = $byear==19 ? '0000' : $byear;
				$bmonth = intval($bmonth);
				$establishtime = $byear.'-'.$bmonth.'-01';
				$db->query("UPDATE ".DB_PRE."member_company SET `establishtime`='$establishtime',`employnum`='$employnum',`turnover`='$turnover' WHERE `userid`='$userid'");
				showmessage('更新成功',$forward);
			}
			else
			{
				$memberinfo = $member->get($_userid, $fields = '*', 1);
				$memberinfo['avatar'] = avatar($_userid);
				@extract(new_htmlspecialchars($memberinfo));
				$data = $member->get_model_info($_userid, $modelid);
				$forminfos = $member_form->get($data);
				$establishtime = explode("-", $company_user_infos['establishtime']);
				$byear = $establishtime[0]== '0000' ? '19' : $establishtime[0];
				$bmonth = $establishtime[1];
				$montharr = array('01','02','03','04','05','06','07','08','09','10','11','12');
			}
			include template('company_edit.html','yellowpage');
		break;
	
		case 'basic':
			if(!$_userid) showmessage('您还没有登陆，即将跳转到登陆页面',url("?module=member&action=login&forward=".urlencode(URL)));
			// common
			if(!isset($action)) $action = 'manage';
			require_once 'form.class.php';
			// common
			
			if($dosubmit)
			{
				extract($_POST);
				$logo = htmlspecialchars($logo);
				$banner = htmlspecialchars($banner);
				$linkman = htmlspecialchars($linkman);
				$email = htmlspecialchars($email);
				$telephone = htmlspecialchars($telephone);
				$fax = htmlspecialchars($fax);
				$zip = intval($zip);
				$introduce = htmlspecialchars($introduce);
				$db->query("UPDATE ".DB_PRE."member_company SET `logo`='$logo',`banner`='$banner',`linkman`='$linkman',`email`='$email',`telephone`='$telephone',`introduce`='$introduce',`fax`='$fax',`zip`='$zip' WHERE `userid`='$_userid'");
				showmessage('更新成功',$forward);
			}
			include template('company_basic.html','yellowpage');
		break;
	
		case 'checksitedomain':
			$userid = $userid ? $userid : $_userid;
			if(!$_userid) showmessage('您还没有登陆，即将跳转到登陆页面',url("?module=member&action=login&forward=".urlencode(URL)));
			// common
			if(!isset($action)) $action = 'manage';
			require_once 'form.class.php';
			// common
			
			if($sitedomain)
			{			
				if(preg_match('/^([a-zA-Z0-9-]+)$/i',$sitedomain))
				{
					if(strlen($sitedomain) < $M['domainlestleth'])
					exit('域名长度不能少于'.$M['domainlestleth'].'个字符');
					else
					{
						$saveDomains = explode("\n",$M['savedDomain']);
						foreach($saveDomains as $p)
						{
							if(trim($sitedomain) == trim($p))exit('对不起，这个域名已经被管理员禁止注册！');
						}
					}
					$r = $db->get_one("SELECT userid FROM `".DB_PRE."member_company` WHERE `sitedomain`='$sitedomain'");
					if($r && $r['userid'] != $_userid)
					{
						exit('该域名已经被其他人占用，请选择其它...');
					}
					else
					{
						$db->query("UPDATE `".DB_PRE."member_company` SET `sitedomain`='$sitedomain' WHERE `userid`='$userid'");
						exit('1');
					}
					
				}
				else
				{
					exit('域名只能包含：小写字母、数字、中划线');
				}
			}
			else
			{
				exit('请输入二级域名，域名只能包含：小写字母、数字、中划线');
			}
			exit;
			include template('company_manage.html','yellowpage');
		break;
		
		case 'search':
			$head['keywords'] .= '企业搜索';
			$head['title'] .= '企业搜索'.'_'.$SETTING['sitename'];
			$head['description'] .= '企业搜索'.'_'.$SETTING['sitename']; 
			$CAT = subcat(0,'yellowpage'); 
			
			include template('company_search.html','yellowpage');
			cache_page(intval($M['cache_list']));
			
		break;
		
		case 'member':
			$company_user_infos = $db->get_one("SELECT * FROM `".DB_PRE."member_company` WHERE `userid`='$_userid'");
			if($company_user_infos)
			{
				showmessage("您已经是企业用户了",$forward);
			}
			else
			{
				$r = $db->get_one("SELECT modelid FROM `".DB_PRE."model` WHERE `tablename`='company' AND `modeltype` = 2");
				$db->query("UPDATE `".DB_PRE."member` SET modelid = '$r[modelid]' WHERE userid = '$_userid'");
				$db->query("UPDATE `".DB_PRE."member` SET modelid = '$r[modelid]' WHERE userid = '$_userid'");
				$db->query("INSERT INTO `".DB_PRE."member_company` (`userid`,`status`) VALUES ('$_userid',1)");
				showmessage("您已成功升级为企业会员！",url("?module=$module&file=$file&action=edit&userid=$_userid"));
			}
			
			include template('company_search.html','yellowpage');
			cache_page(intval($M['cache_list']));
		break;
		
		case 'searchlist':
			$head['keywords'] .= '企业搜索结果';
			$head['title'] .= '企业搜索结果'.'_'.$SETTING['sitename'];
			$head['description'] .= '企业搜索结果'.'_'.$SETTING['sitename'];
			$page = $page ? $page:1;
			$catid = intval($catid);
			$q = addslashes(htmlspecialchars($q));
			$genre = addslashes(htmlspecialchars($genre));
			$areaname = addslashes(htmlspecialchars($areaname));
			$where = '';
			if($catid)
			{
				$c= new company();
				$r = $c->get_yp_arrchildid($catid);
				if($r['arrchildid'])$r['arrchildid'] = $catid.','.$r['arrchildid'];
				else $r['arrchildid'] = $catid;
				$where .= "r.catid IN ($catid) AND ";
			}
			if($genre) $where .= "c.genre = '{$genre}' AND ";
			if($areaname) $where .= "c.areaname = '{$areaname}' AND ";
			if($q) $where .= "c.companyname LIKE '%{$q}%' AND ";
			$where .= "r.userid = c.userid AND c.status = 1"; 
			
			include template('company_searchlist.html','yellowpage');
			cache_page(intval($M['cache_list']));
		break;
		
		default: 
			$head['keywords'] .= '企业';
			$head['description'] .= '企业'.'_'.$SETTING['sitename'];
			$head['title'] .= '企业'.'_'.$SETTING['sitename'];
			$c= new company();
			$r = $c->get_yp_arrchildid($catid);
			if($r['arrchildid'])
			{
				$r['arrchildid'] = $catid.','.$r['arrchildid'];
			}
			else
			{
				$r['arrchildid'] = $catid;
			}
			if($CATEGORY[$catid])
			{
				$where .= "r.catid IN ({$r['arrchildid']})";
			}
			$page = intval($page);
			if(!$page)$page = 1;
			if($areaname)
			{
				$areaname = htmlspecialchars($areaname);
				if(strpos(',',$areaname)===false)
				{
					if($where)$where .= " AND c.areaname='$areaname'";
					else $where .= " c.areaname='$areaname'";
				}
				else
				{
					if($where)$where .= " AND c.areaname IN ($areaname)";
					else $where .= " c.areaname IN ($areaname)";
				}
				$areaname = urlencode($areaname);
			}
			if($where) $where .= " AND c.status = '1'";
			else $where = "c.status = '1'";
			$pagesize = intval($pagesize);
			if(!$pagesize) $pagesize = 20;
			switch($listtype)
			{
				case 'elite':
				$where .= " AND c.elite = 1";
				break;
				
				case 'new':
				$listtype = 'new';
				break;
				
				default:
				$listtype = "all";
				break;
			}
			include template('company.html','yellowpage');
			cache_page(intval($M['cache_list']));
		break;
	}
?>